/**
 * 1945. 字符串转化后的各位数字之和
 * https://leetcode-cn.com/problems/sum-of-digits-of-string-after-convert/
 */
public class Solutions_1945 {
    public static void main(String[] args) {
//        String s = "iiii";
//        int k = 1;  // output: 36

        String s = "leetcode";
        int k = 2;  // output: 6

        int result = getLucky(s, k);
        System.out.println(result);
    }

    public static int getLucky(String s, int k) {
        char[] arr = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char c : arr) {
            // 字母转换为数字
            sb.append(c - 'a' + 1);
        }

        String str = sb.toString();
        while (k > 0) {
            int sum = 0;
            for (int i = 0; i < str.length(); i++) {
                // 逐位相加
                sum += str.charAt(i) - '0';
            }
            str = String.valueOf(sum);
            k --;
        }
        return Integer.valueOf(str);
    }
}
